﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServBus.ListForm2010.Setup.Validators;

namespace ServBus.ListForm2010.Setup.Controls
{
    public partial class ValidationControl : UserControl
    {
        private System.Timers.Timer timer;
        private delegate void ValidateCompletedHandler();
        private delegate void ValidateStartedHandler();

        public BaseValidator Validator { get; private set; }

        public ValidationControl()
        {
            InitializeComponent();
        }

        public ValidationControl(BaseValidator validator, System.Timers.Timer timer)
        {
            InitializeComponent();
            this.Validator = validator;
            this.timer = timer;
            this.labelText.Text = validator.CheckItemText;
        }

        internal void ResetControlState()
        {
            this.labelImage.Image = Properties.Resources.CheckWait;
            this.labelText.Text = this.Validator.CheckItemText;
        }

        void ValidateStarted()
        {
            this.labelImage.Image = Properties.Resources.Checking;
        }

        void ValidateCompleted()
        {
            if (this.Validator.Validated)
            {
                this.labelImage.Image = Properties.Resources.CheckPass;
                this.labelText.Text = this.Validator.ValidText;
            }
            else
            {
                this.labelImage.Image = Properties.Resources.CheckFail;
                this.labelText.Text = this.Validator.InvalidText;
            }
            timer.Start();
        }

        public void RunValidate()
        {
            this.BeginInvoke(new ValidateStartedHandler(ValidateStarted));
            this.Validator.Validate();
            this.BeginInvoke(new ValidateCompletedHandler(ValidateCompleted));
        }
    }
}
